clear
set more off

* All required add-ons are stored in the /packages and /auxiliary folders
adopath ++ "$Wellness_WhatDoesWWDo/scripts/packages"
adopath ++ "$Wellness_WhatDoesWWDo/scripts/auxiliary"
mata: mata mlib index

******************************************
** Create Figure 1: Experimental Design **
******************************************

* Load Analysis sample

use "$Wellness_WhatDoesWWDo/data/proc/wellness_analysis.dta", clear

* Initial Pool = 12,459
count 
local initial = string(r(N),"%9.0fc")

* Study Sample = 4,834
count if StudyArm < 8
local core = string(r(N),"%9.0fc")

* Control Group = 1,534
count if StudyArm == 7
local control = string(r(N),"%9.0fc")

* Treatment Group = 3,300
count if StudyArm < 7
local treatment = string(r(N),"%9.0fc")

* Treatment Biometric Screening + HRA Year 1 = 1,848
count if StudyArm < 7 & hra_c == 1
local tscreen_1 = string(r(N),"%9.0fc")

* Treatment Fall Activities 2016 = 903
count if StudyArm < 7 & activity_f_c == 1
local tfall_1 = string(r(N),"%9.0fc")

* Treatment Spring Activities 2016 = 740
count if StudyArm < 7 & activity_s_c == 1
local tspring_1 = string(r(N),"%9.0fc")

* Control Follow Up Survey 2017 = 1,157
count if StudyArm == 7 & survey2017_c == 1
local csurvey_2 = string(r(N),"%9.0fc")

* Treatment Follow Up Survey 2017 = 2,410
count if StudyArm < 7 & survey2017_c == 1
local tsurvey_2 = string(r(N),"%9.0fc")

* Control Screening 2017 = 595
count if StudyArm == 7 & screening2017_c == 1
local cscreen_2 = string(r(N),"%9.0fc")

* Treatment Screening + HRA 2017 = 1,272
count if StudyArm < 7 & hra_c_yr2 == 1
local tscreen_2 = string(r(N),"%9.0fc")

* Treatment Fall Activities 2016 = 439
count if StudyArm < 7 & activity_f_c_yr2 == 1
local tfall_2 = string(r(N),"%9.0fc")

* Treatment Spring Activities 2016 = 342
count if StudyArm < 7 & activity_s_c_yr2 == 1
local tspring_2 = string(r(N),"%9.0fc")

* Control Survey 2018 = 991
count if StudyArm == 7 & survey2018_c == 1
local csurvey_3 = string(r(N),"%9.0fc")

* Treatment Survey 2018 = 2,029
count if StudyArm < 7 & survey2018_c == 1
local tsurvey_3 = string(r(N),"%9.0fc")

* Control Screening 2018 = 557
count if StudyArm == 7 & screening2018_c == 1
local cscreen_3 = string(r(N),"%9.0fc")

* Treatment Screening 2018 = 1,204
count if StudyArm < 7 & screening2018_c == 1
local tscreen_3 = string(r(N),"%9.0fc")


* Must be called using the command texdoc do "5b. make_figure1.do"
* Currently called from "5. make_figures.do"

texdoc init "$Wellness_WhatDoesWWDo/results/figures/experimental_design.tex", replace

* Macros for Figure

texdoc substitute @initial "`initial'" @core "`core'" @control "`control'" ///
	@treatment "`treatment'" @tscreen_1 "`tscreen_1'" @tfall_1 "`tfall_1'" ///
	@tspring_1 "`tspring_1'" @csurvey_2 "`csurvey_2'" ///
	@tsurvey_2 "`tsurvey_2'" @cscreen_2 "`cscreen_2'" ///
	@tscreen_2 "`tscreen_2'" @tfall_2 "`tfall_2'" @tspring_2 "`tspring_2'" ///
	@csurvey_3 "`csurvey_3'" @tsurvey_3 "`tsurvey_3'" ///
	@cscreen_3 "`cscreen_3'" @tscreen_3 "`tscreen_3'"

/*tex
\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{ulem}
\usepackage[hmargin=1cm,vmargin=2cm]{geometry}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,fit,positioning,shapes.symbols,chains,calc,external}
\usetikzlibrary{intersections}
\usetikzlibrary{external}
\usetikzlibrary{backgrounds}

\tikzexternalize

\begin{document}

\tikzset{external/force remake}

\begin{tikzpicture}
[node distance = 1cm, auto,font=\footnotesize,
% STYLES
every node/.style={node distance=2cm},
% This style is used to draw the group's name
group/.style={rectangle, draw, fill=black!10, inner sep=5pt, text width=4cm, text badly centered, minimum height=1.2cm, font=\bfseries\footnotesize\rmfamily},
% This style is used to draw the subgroups name
subgroup/.style={rectangle, draw, fill=black!10, inner sep=5pt, text width=1cm, text badly centered, minimum height=1.2cm, font=\bfseries\footnotesize\rmfamily}] 

%% Draw Initial Pool, Core Sample, & Control Group
\node [group,text width=5cm] (initial) {Initial Pool: \\ Benefits-Eligible Employees \\ (@initial)};
\node [group, below of = initial,dashed,text width=5cm] (core) {Baseline Survey: \\ (@core)};
\node [below = 1.5cm of core] (core_a) {};
\node [group, text width=4cm, right = 1cm of core_a] (treatment) {Treatment Group \\ (@treatment)};
\node [group, text width=4cm,  left = 2cm of treatment] (control) {Control Group \\ (@control)};

% Draw Year 1 Treatment
\node [group,below of = treatment,dashed, text width=4.5cm] (tscreen_1) {Biometric Screening + HRA \\ (@tscreen_1)};
\node [group,below of = tscreen_1,text width = 5cm] (tactivities_1) {Fall Wellness Activity (@tfall_1) \\
													Spring Wellness Activity (@tspring_1)};
													
% Draw Year 2 Survey
\node [group, below = 1cm of tactivities_1, text width=4cm] (tsurvey_2) {Follow-Up Survey \\ (@tsurvey_2)};
\node [group, left = 2cm of tsurvey_2, text width=4cm] (csurvey_2) {Follow-Up Survey \\ (@csurvey_2)};

% Draw Year 2 Screening
\node [group, dashed, below = 1cm of tsurvey_2, text width=4.5cm] (tscreen_2) {Biometric Screening + HRA \\ (@tscreen_2)};
\node [group, left = 1.75cm of tscreen_2, text width = 4cm] (cscreen_2) {Biometric Screening \\ (@cscreen_2)};

% Draw Year 2 Treatment
\node [group, below of = tscreen_2, text width = 5cm] (tactivities_2) {Fall Wellness Activity (@tfall_2) \\
													Spring Wellness Activity (@tspring_2)};
% Draw Final Survey
\node [group, below = 1cm of tactivities_2, text width=4cm] (tsurvey_3) {Follow-Up Survey \\ (@tsurvey_3)};
\node [group, left = 2cm of tsurvey_3, text width=4cm] (csurvey_3) {Follow-Up Survey \\ (@csurvey_3)};

% Draw Final Screening
\node [group, below = 1cm of tsurvey_3, text width = 4cm] (tscreen_3) {Biometric Screening \\ (@tscreen_3)};
\node [group, left = 2cm of tscreen_3, text width = 4cm] (cscreen_3) {Biometric Screening \\ (@cscreen_3)};

%% Guides for connecting groups
\node [above = .9cm of core_a] (core_b) {};
\node [right =1cm of treatment] (treatment_a) {};
\node[above = .5cm of tsurvey_2] (tsurvey_2a) {};
\node[above = .5cm of tscreen_2] (tscreen_2a) {};
\node[above = .5cm of cscreen_2] (cscreen_2a) {};
\node[above = .5cm of tsurvey_3] (tsurvey_3a) {};
\node[above = .5cm of csurvey_3] (csurvey_3a) {};
\node[above = .5cm of tscreen_3] (tscreen_3a) {};
\node[above = .5cm of cscreen_3] (cscreen_3a) {};


%% Connect the Dots
\draw[->,dashed] (initial) -- (core);
\draw (core) -- (core_b.center);
\draw[->] (core_b.center) -| (control);
\draw[->] (core_b.center) -| (treatment);

\draw[->,dashed] (treatment) -- (tscreen_1);
\draw[->,dashed] (tscreen_1) -- (tactivities_1);
\draw[->,dashed] ($(treatment.east) + (0,-.375cm)$) -- ($(treatment.east) + (.8cm,-.375cm)$) |- (tsurvey_2a) -- (tsurvey_2);
\draw[->,dashed] ($(treatment.east) + (0,-.125cm)$) -- ($(treatment.east) + (1cm,-.125cm)$) |- (tscreen_2a) -- (tscreen_2);
\draw[->,dashed] (tscreen_2) -- (tactivities_2);
\draw[->,dashed] ($(treatment.east) + (0,.125cm)$) -- ($(treatment.east) + (1.2cm,.125cm)$) |- (tsurvey_3a) -- (tsurvey_3);
\draw[->,dashed] ($(treatment.east) + (0,.375cm)$) -- ($(treatment.east) + (1.4cm,.375cm)$) |- (tscreen_3a) -- (tscreen_3);

\draw[->,dashed] (control) -- (csurvey_2);
\draw[->,dashed] ($(control.east) + (0,-.25cm)$) -- ($(control.east) + (.8cm,-.25cm)$) |- (cscreen_2a) -- (cscreen_2);
\draw[->,dashed] (control) -- ($(control.east) + (1cm,0)$) |- (csurvey_3a) -- (csurvey_3);
\draw[->,dashed] ($(control.east) + (0,.25cm)$) -- ($(control.east) + (1.2cm,.25cm)$) |- (cscreen_3a) -- (cscreen_3);

%% Legend

\node [subgroup, dashed, minimum height = .5cm, text width = .5cm,right = 1cm of initial] (dash_box) {};
\node [right = .5cm of dash_box,align = left] {Continuation conditional \\ on completion};
\node [below = .5cm of dash_box, subgroup, minimum height = .5cm, text width = .5cm,draw = none,fill = none] (reg_arrow) {};
\draw[->] (reg_arrow.west) -- (reg_arrow.east);
\node [right = .5cm of reg_arrow,align = left] {Random assignment};
\node [below = .5cm of reg_arrow, subgroup, minimum height = .5cm, text width = .5cm,draw = none,fill = none] (dash_arrow) {};
\draw[->,dashed] (dash_arrow.west) -- (dash_arrow.east);
\node [right = .5cm of dash_arrow,align = left] {Continuation by \\ choice};

%% Timeline

\node [left= 2cm,font=\normalsize, text width=3cm] (timeline) at (initial.west) {\uline{Timeline:}};
\draw (timeline |- control) node[font=\normalsize, text width=3cm] (summer16) {Summer \\ 2016};
\draw (timeline |- tactivities_1) node[font=\normalsize, text width=3cm] (fall16) {Fall 2016 --\\ Spring 2017};
\node (follow_1) at ($(tsurvey_2)!0.5!(tscreen_2)$) {};
\draw (timeline |- follow_1) node[font=\normalsize, text width=3cm] (summer17) {Summer \\ 2017};
\draw (timeline |- tactivities_2) node[font=\normalsize, text width=3cm] (fall17) {Fall 2017 --\\ Spring 2018};
\node (follow_2) at ($(tsurvey_3)!0.5!(tscreen_3)$) {};
\draw (timeline |- follow_2) node[font=\normalsize, text width=3cm] (summer18) {Summer \\ 2018};

\end{tikzpicture}

\end{document}
tex*/

texdoc close
